package com.ruoyi.patrol.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.patrol.domain.PtFormDetails;
import com.ruoyi.patrol.domain.PtFormFilling;
import com.ruoyi.patrol.domain.vo.PtFormDetailVo;
import com.ruoyi.patrol.mapper.PtFormFillingMapper;
import com.ruoyi.patrol.service.IPtFormDetailsService;
import com.ruoyi.patrol.service.IPtFormFillingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.patrol.mapper.PtFormReplyMapper;
import com.ruoyi.patrol.domain.PtFormReply;
import com.ruoyi.patrol.service.IPtFormReplyService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 单回复详情Service业务层处理
 *
 * @author lkm
 * @date 2022-11-14
 */
@Service
public class PtFormReplyServiceImpl implements IPtFormReplyService {
    @Autowired
    private PtFormReplyMapper ptFormReplyMapper;

    @Autowired
    private IPtFormFillingService iPtFormFillingService;


    /**
     * 查询单回复详情
     *
     * @param id 单回复详情ID
     * @return 单回复详情
     */
    @Override
    public PtFormReply selectPtFormReplyById(Long id) {
        return ptFormReplyMapper.selectPtFormReplyById(id);
    }

    /**
     * 查询单回复详情列表
     *
     * @param ptFormReply 单回复详情
     * @return 单回复详情
     */
    @Override
    public List<PtFormReply> selectPtFormReplyList(PtFormReply ptFormReply) {
        return ptFormReplyMapper.selectPtFormReplyList(ptFormReply);
    }

    /**
     * 新增单回复详情
     *
     * @param ptFormDetailVo 表单回复添加数据集合
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPtFormReply(PtFormDetailVo ptFormDetailVo) {
        PtFormFilling formFilling = iPtFormFillingService.selectPtFormFillingById(ptFormDetailVo.getFormFillingId());
        if (formFilling.getFillType() == 1) {
            throw new RuntimeException("该表单已填写");
        }
        PtFormFilling ptFormFilling = new PtFormFilling();
        ptFormFilling.setId(ptFormDetailVo.getFormFillingId());
        ptFormFilling.setUpdateBy(ptFormDetailVo.getFillinperson());
        ptFormFilling.setFillTime(new Date());
        ptFormFilling.setFillType(1L);
        iPtFormFillingService.updatePtFormFilling(ptFormFilling);
        List<PtFormReply> replyList = new ArrayList<>();
        ptFormDetailVo.getFormlist().forEach(v -> {
            PtFormReply ptFormReply = new PtFormReply();
            ptFormReply.setFormId(formFilling.getFormId());
            ptFormReply.setSchoolId(formFilling.getSchoolId());
            ptFormReply.setFromDetailsId(v.getKey());
            if (v.getType() == 1) {
                String[] reply = {""};
                String[] reason = {""};
                v.getOptionlist().forEach(y -> {
                    if (y.isChecked()) {
                        reply[0] += y.getKey() + "#";
                    }else {
                        reply[0] +="#";
                    }
                    if (y.isReason()) {
                        reason[0] += y.getReasontext() + "#";
                    } else {
                        reason[0] += "#";
                    }
                });
                ptFormReply.setReply(reply[0]);
                ptFormReply.setReplyReason(reason[0]);
            } else {
                ptFormReply.setReply(v.getReply());
                ptFormReply.setReplyReason("");
            }
            replyList.add(ptFormReply);
        });
        ptFormReplyMapper.insertPtFormReply(replyList);
        return 1;
    }

    /**
     * 修改单回复详情
     *
     * @param ptFormReply 单回复详情
     * @return 结果
     */
    @Override
    public int updatePtFormReply(PtFormReply ptFormReply) {
        ptFormReply.setUpdateTime(DateUtils.getNowDate());
        return ptFormReplyMapper.updatePtFormReply(ptFormReply);
    }

    /**
     * 批量删除单回复详情
     *
     * @param ids 需要删除的单回复详情ID
     * @return 结果
     */
    @Override
    public int deletePtFormReplyByIds(Long[] ids) {
        return ptFormReplyMapper.deletePtFormReplyByIds(ids);
    }

    /**
     * 删除单回复详情信息
     *
     * @param id 单回复详情ID
     * @return 结果
     */
    @Override
    public int deletePtFormReplyById(Long id) {
        return ptFormReplyMapper.deletePtFormReplyById(id);
    }
}
